<?php

namespace App\Http\Controllers\api;

use App\Friend;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class FriendController extends Controller
{

    // 保存朋友圈
    function save(Request $request){

        //return $request->file[1];

    // 1 将文字存入字段;
        $friend = new Friend();
        $user =  \Auth::guard("api")->user();
        $friend->user_id = $user->id;
        $friend->content = $request->content;
        $friend->imgs = $this->upload($request->file);
        $res = $friend->save();

        return  response()->json(["code"=>"200","msg"=>"保存成功","res"=>$res]);

    }

    // 图片上传
    private function upload($files){

        // 1 上传图片路径
        $path = base_path("public/upload/");

        $img_str = "";

        foreach ($files as $file){
            $ext = $file->getClientOriginalExtension();  //扩展名 girl_0.jpg
            $file_name = uniqid() . "." . $ext;            //重命名上传文件 12345.jpg
            $file->move($path, $file_name); //移动至指定目录
            $img_str.=",".$file_name;
        }

        return $img_str;

    }


}
